package com.kaikeba.controller;

import com.kaikeba.bean.Message;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.AdminService;
import com.kaikeba.service.impl.AdminServiceImpl;
import com.kaikeba.util.JsonUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class AdminController {

    AdminService adminService = new AdminServiceImpl();

    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest req, HttpServletResponse res){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        boolean login = adminService.login(username, password);

        Message message = null;
        if (login){
            message = new Message(0,"登录成功");
            req.getSession().setAttribute("userName",username);
            Date date = new Date();
            String ip = req.getRemoteAddr();
            adminService.updateLoginTime(username,ip,date);
        }else {
            message = new Message(0,"登录失败");
        }

        return JsonUtil.toJson(message);
    }
}
